package vip.zhenzicheng.concurrent_program.thread.sgg.completablefuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * 合并结果
 *
 * @author zhenzicheng
 * @date 2022-06-21 20:19
 */
public class CompletableFutureCombineDemo {

  public static void main(String[] args) {
    CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
      try {
        TimeUnit.SECONDS.sleep(1);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      return 10;
    });

    CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
      try {
        TimeUnit.SECONDS.sleep(2);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      return 20;
    });

    CompletableFuture<Integer> result = future1.thenCombine(future2, (result1, result2) -> {
      System.out.println("----开始合并");
      return result1 + result2;
    });

    System.out.println(result.join());
  }
}
